﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibrary;

namespace ObTookit.Auto
{
    public class ServCoder : BaseCoder
    {
        public string CreateCode()
        {
            FieldObject pkFO = GetIdObject();
            string text = ReadTemplate(Template);
            text = text.Replace("[#BaseNS#]", AutoObj.BaseNS);
            text = text.Replace("[#ModelNS#]", AutoObj.OmNS);
            text = text.Replace("[#LgNS#]", AutoObj.LgNS);
            text = text.Replace("[#AUTHOR#]", Author);
            text = text.Replace("[#CREATED#]", CreateDate);

            text = text.Replace("[#ServNS#]", AutoObj.ServNS);
            text = text.Replace("[#ClsBaseServ#]", AutoObj.ServBaseCls);
            text = text.Replace("[#ClsName#]", AutoObj.ClsName);
            text = text.Replace("[#ClsBaseSession#]", AutoObj.BaseSession);

            text = text.Replace("[#sClsName#]", StringUtil.FirstChar(AutoObj.ClsName, "_", false));
            text = text.Replace("[#dbFields#]", CreateFieldString());
            text = text.Replace("[#IdAlias#]", pkFO.Alias);
            text = text.Replace("[#IdName#]", pkFO.Name);
            text = text.Replace("[#IdSysType#]", GetIdType(pkFO.Type));
            text = text.Replace("[#TryGetType#]", GetTryGetType(pkFO.Type));
            text = text.Replace("[#TryGetFirstType#]", GetTryGetFirstType(pkFO.Type));
            return text;
        }

        private string CreateFieldString()
        {
            StringBuilder dbFields = new StringBuilder();
            foreach(FieldObject fo in FieldObjs) {
                dbFields.Append(fo.Alias + ",");
            }
            return dbFields.Remove(dbFields.Length - 1, 1).ToString();
        }
    }
}
